package com.ncmem.up7.biz;
import java.io.IOException;

import com.ncmem.up7.ConfigReader;
import com.ncmem.up7.model.FileInf;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;

public class PathBuilder {

    public PathBuilder(){}

    /**
     * 获取上传路径
     * 格式：
     * 	webapp_name/upload
     * @return
     * @throws IOException
     */
    public String getRoot() throws IOException{

        String root = new String("");
        //前面会多返回一个"/", 例如  /D:/test/WEB-INF/, 奇怪, 所以用 substring()
        root = this.getClass().getResource("/").getPath().substring(1).replaceAll("//", "/");
        if ( !StringUtils.isBlank(root) && !root.endsWith("/"))
        {
            root = root.concat("/");    //避免 WebLogic 和 WebSphere 不一致
        }
        root = root.replace("classes/", "");
        //D:/apache-tomcat-6.0.29/webapps/Uploader6.1MySQL/WEB-INF/
        root = root.replace("%20", " ");//fix(2016-02-29):如果路径中包含空格,getPath会自动转换成%20
        //D:/apache-tomcat-6.0.29/webapps/Uploader6.1MySQL
        root = root.replace("WEB-INF/", "");

        ConfigReader cr = new ConfigReader();
        JSONObject jo =  cr.module("path");
        String pathSvr = jo.getString("upload-folder");
        pathSvr = pathSvr.replace("{root}", root);
        pathSvr = pathSvr.replaceAll("\\\\", "/");
        return pathSvr;

    }
    public String genFolder(int uid,String id,String nameLoc) throws IOException{return "";}
    public String genFile(int uid, FileInf f) throws IOException{return "";}
    public String genFile(int uid,String md5,String nameLoc)throws IOException{return "";}

}
